#include "MaterialCacheData.h"



MaterialCacheData::MaterialCacheData()
	: m_maxCount(0)
{
}


MaterialCacheData::~MaterialCacheData()
{
	Clear();
}

bool MaterialCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void MaterialCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool MaterialCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/item/material.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/item/material.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}


void MaterialCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		Material* p = new Material;
		_safe_check(p);

		p->mId = jm["props_id"].asUInt();
		strcpy(p->mName, jm["name"].asString().c_str());
		p->mTypes = jm["types"].asUInt();
		strcpy(p->mDescribe, jm["describe"].asString().c_str());
		p->mGrade = jm["grade"].asUInt();
		p->mNextGrade = jm["next_grade"].asUInt();
		p->mSprice = jm["sprice"].asUInt();

		p->mSynCost = jm["syn_cost"].asUInt();
		p->mDropFb = jm["drop_fb"].asUInt();
		strcpy(p->mIconName, jm["icon_name"].asString().c_str());
		p->mReleased = jm["released"].asUInt();
		p->mLvLimit = jm["lv_limit"].asUInt();
		p->mPiles = jm["piles"].asUInt();

		// [0,0,0,0,0]
		Json::Value cprice = jm["cprice"];
		for (uint32_t i = 0; i < max_money_type; ++i)
		{
			p->mCPrice[i] = cprice[i].asUInt();
		}

		// [[102000100,5],[102000200,5]]
		Json::Value synCondition = jm["syn_condition"];
		uint32_t sz = synCondition.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			Json::Value vv = synCondition[i];
			uint32_t szz = vv.size();
			Condition c;
			for (uint32_t n = 0; n < szz; ++n)
			{
				c.mValue[n] = vv[n].asUInt();
			}
			p->mSynCondition.push_back(c);
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

Material* MaterialCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}

	_safe_guard_end;

	return nullptr;
}

bool MaterialCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	Material* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error10");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);
	_jm["name"] = Json::Value(p->mName);
	_jm["types"] = Json::Value(p->mTypes);
	_jm["describe"] = Json::Value(p->mDescribe);
	_jm["grade"] = Json::Value(p->mGrade);
	_jm["next_grade"] = Json::Value(p->mNextGrade);
	_jm["sprice"] = Json::Value(p->mSprice);
	_jm["syn_cost"] = Json::Value(p->mSynCost);
	_jm["drop_fb"] = Json::Value(p->mDropFb);

	_jm["icon_name"] = Json::Value(p->mIconName);
	_jm["released"] = Json::Value(p->mReleased);
	_jm["lv_limit"] = Json::Value(p->mLvLimit);
	_jm["piles"] = Json::Value(p->mPiles);

	
	Json::Value cprice;
	for (uint32_t i = 0; i < max_money_type; ++i)
	{
		cprice.append(p->mCPrice[i]);
	}
	_jm["cprice"] = cprice;

	Json::Value synCondition;
	for (auto it = p->mSynCondition.begin(); it != p->mSynCondition.end(); ++it)
	{
		Json::Value tmp;
		if ((*it).mValue)
		{
			int len = sizeof((*it).mValue) / sizeof((*it).mValue[0]);
			for (int i = 0; i < len; ++i)
			{
				if ((*it).mValue[i])
				{
					tmp.append((*it).mValue[i]);
				}
			}

			synCondition.append(tmp);
		}
	}
	_jm["syn_condition"] = synCondition;

	return true;

	_safe_guard_end;
}

uint32_t MaterialCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}